package com.hsjk.szwj.module.smedia.service.template;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.GetTemplateMaterialsRequest;
import com.aliyun.ice20201109.models.GetTemplateMaterialsResponse;
import com.aliyun.ice20201109.models.GetTemplateParamsRequest;
import com.aliyun.ice20201109.models.GetTemplateParamsResponse;
import com.aliyun.ice20201109.models.ListTemplatesRequest;
import com.aliyun.ice20201109.models.ListTemplatesResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.module.smedia.controller.admin.template.vo.TemplateMaterialsReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.template.vo.TemplateMaterialsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.template.vo.TemplateParamsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.template.vo.TemplatesReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.template.vo.TemplatesRespVO;
import com.hsjk.szwj.module.smedia.convert.template.TemplateConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * @author chenjiawei
 * @desc 模板相关接口实现类
 * @Date 2023/07/21 11:15
 **/
@Service
@Validated
@Slf4j
public class TemplateImpl implements ITemplate {

    @Value("${ali.media.access-key-id}")
    private String accessKeyId;

    @Value("${ali.media.access-key-secret}")
    private String accessKeySecret;

    @Value("${ali.media.endpoint}")
    private String endpoint;

    @Override
    public TemplatesRespVO listTemplates(TemplatesReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        TemplatesRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            ListTemplatesRequest request = new ListTemplatesRequest();
            request.setPageNo(reqVO.getPageNo());
            request.setPageSize(reqVO.getPageSize());
            if (ObjectUtil.isNotEmpty(reqVO.getStatus())) {
                request.setStatus(reqVO.getStatus());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getKeyword())) {
                request.setKeyword(reqVO.getKeyword());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getType())) {
                request.setType(reqVO.getType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getCreateSource())) {
                request.setCreateSource(reqVO.getCreateSource());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getSortType())) {
                request.setSortType(reqVO.getSortType());
            }
            ListTemplatesResponse templatesResponse = client.listTemplatesWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(templatesResponse)) {
                response = TemplateConvert.INSTANCE.convertTemplatesRespVO(templatesResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public TemplateMaterialsRespVO getTemplateMaterials(TemplateMaterialsReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        TemplateMaterialsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetTemplateMaterialsRequest request = new GetTemplateMaterialsRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateId())) {
                request.setTemplateId(reqVO.getTemplateId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getFileList())) {
                request.setFileList(reqVO.getFileList());
            }

            GetTemplateMaterialsResponse materialsWithOptions = client.getTemplateMaterialsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(materialsWithOptions)) {
                response = TemplateConvert.INSTANCE.convertTemplateMaterialsRespVO(materialsWithOptions.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public TemplateParamsRespVO getTemplateParams(String templateId) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        TemplateParamsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetTemplateParamsRequest request = new GetTemplateParamsRequest();
            if (ObjectUtil.isNotEmpty(templateId)) {
                request.setTemplateId(templateId);
            }
            GetTemplateParamsResponse paramsWithOptions = client.getTemplateParamsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(paramsWithOptions)) {
                response = TemplateConvert.INSTANCE.convertTemplateParamsRespVO(paramsWithOptions.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }
}
